package com.nit.lab.service.ship.Impl;


import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.ship.ShipDTO;
import com.nit.lab.entity.ship.ShipEntity;
import com.nit.lab.mapper.ship.ShipMapper;
import com.nit.lab.service.ship.ShipService;

import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;

@Service
public class ShipServiceImpl implements ShipService{

    @Autowired(required = false)
    private ShipMapper shipMapper;

    @Override
    public OpResultDTO queryShip() throws Exception {
        OpResultDTO opResultDTO = new OpResultDTO();
        opResultDTO.setResult(shipMapper.queryShip());
        return opResultDTO;
    }

    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        Long count = shipMapper.count4Table(
                tableReqDTO.parseQueryParam("shipName"),
                tableReqDTO.parseQueryParam("mmsiNumber"),
                tableReqDTO.parseQueryParam("countryId"),
                tableReqDTO.parseQueryParam("typeId"),
                tableReqDTO.parseQueryParam("beginPortId"),
                tableReqDTO.parseQueryParam("targetPortId"),
                tableReqDTO.parseQueryParam("gradeId"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                shipMapper.list4Table(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("isInport"),
                        tableReqDTO.parseQueryParam("shipName"),
                        tableReqDTO.parseQueryParam("mmsiNumber"),
                        tableReqDTO.parseQueryParam("countryId"),
                        tableReqDTO.parseQueryParam("typeId"),
                        tableReqDTO.parseQueryParam("beginPortId"),
                        tableReqDTO.parseQueryParam("targetPortId"),
                        tableReqDTO.parseQueryParam("gradeId"),
                        tableReqDTO.parseQueryParam("categoryId"),
                        tableReqDTO.parseQueryParam("epidemicTypeId"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    public TableRspDTO list4LeaveTable(TableReqDTO tableReqDTO) throws Exception {
        Long count = shipMapper.count4Table(
                tableReqDTO.parseQueryParam("shipName"),
                tableReqDTO.parseQueryParam("mmsiNumber"),
                tableReqDTO.parseQueryParam("countryId"),
                tableReqDTO.parseQueryParam("typeId"),
                tableReqDTO.parseQueryParam("beginPortId"),
                tableReqDTO.parseQueryParam("targetPortId"),
                tableReqDTO.parseQueryParam("gradeId"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                shipMapper.list4LeaveTable(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("isInport"),
                        tableReqDTO.parseQueryParam("shipName"),
                        tableReqDTO.parseQueryParam("mmsiNumber"),
                        tableReqDTO.parseQueryParam("countryId"),
                        tableReqDTO.parseQueryParam("typeId"),
                        tableReqDTO.parseQueryParam("beginPortId"),
                        tableReqDTO.parseQueryParam("targetPortId"),
                        tableReqDTO.parseQueryParam("gradeId"),
                        tableReqDTO.parseQueryParam("categoryId"),
                        tableReqDTO.parseQueryParam("epidemicTypeId"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(ShipDTO shipDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        ShipEntity shipEntity = mapperFactory.getMapperFacade().map(shipDTO,ShipEntity.class);
        shipEntity.setGmtCreate(new Date());
        shipEntity.setDeclareTime(new Date()); //申报时间直接设置为当前时间 简便操作
        return shipMapper.add(shipEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(ShipDTO shipDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        ShipEntity shipEntity = mapperFactory.getMapperFacade().map(shipDTO, ShipEntity.class);
        shipEntity.setGmtModified(new Date());
        return shipMapper.update(shipEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String shipIds) throws Exception {
        String[] arrayShipId = shipIds.split(",");
        for (int i = 0; i < arrayShipId.length; i++) {
            shipMapper.remove(Long.parseLong(arrayShipId[i]));
        }
        return arrayShipId.length;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer leavePort(ShipDTO shipDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        ShipEntity shipEntity = mapperFactory.getMapperFacade().map(shipDTO, ShipEntity.class);
        shipEntity.setGmtModified(new Date());
        return shipMapper.leavePort(shipEntity);
    }

    @Override
    public ShipDTO getByShipId(Long shipId) throws Exception {
        return shipMapper.getByShipId(shipId);
    }

    @Override
    public ShipDTO getByMssiNumber(String mmsiNumber) throws Exception {
        return shipMapper.getByMmsiNumber(mmsiNumber);
    }

}
